package org.nf.trading.controller.admin.user;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.dao.UserMapper;
import org.nf.trading.entity.User;
import org.nf.trading.service.app.user.UserService;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin()
@RestController
@RequestMapping("/admin/user")
@Slf4j
@RequiredArgsConstructor
public class UserManagementController extends BaseController {
    private final UserService userService;
    private final UserMapper dao;

    //查询所有的用户
    @GetMapping("/selectUserAll")
    public ResultVO selectUserAll(Integer pageNum, Integer pageSize, String userName,String phone) {
        ResultVO vo = new ResultVO();
        vo.setData(userService.selectUserAll(pageNum, pageSize, userName,phone));
        return vo;
}

    //通过用户id查询用户的基本信息（包括用户的发布的数量和下单的数量）


    //删除用户
    @PostMapping("/delUser")
    public ResultVO delUser(@RequestBody User user){
        dao.deleteByPrimaryKey(user.getUserId());
        ResultVO vo = success();
        return vo;
    }

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public ResultVO addUser(User user) {
        user.setUserDatetime(new Date());
        user.setUserPwd("123456");
        // 如果前端没有上传图片，那么就添加默认的图片信息
        if (user.getUserAvatar() == null || user.getUserAvatar().isEmpty()){
            user.setUserAvatar("default.jpg");
        }
        dao.addUser(user);
        ResultVO vo = success();
        return vo;
    }

    /**
     * 重置密码
     */
    @PostMapping("/updatePwd")
    public ResultVO updatePwd(@RequestBody User user) {
        dao.updatePwd(user.getUserId());
        ResultVO vo = success();
        return vo;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public ResultVO updateUser(User user) {
        dao.updateUser(user);
        ResultVO vo = success();
        return vo;
    }
}
